# 乘积最大子数组

class Solution:

    def maxProduct(self, nums):

        if not nums:
            return
        if len(nums) == 1:
            return nums[0]
        
        # 维护当前值、最大值、最小值
        p = nums[0]
        maxP = nums[0]
        minP = nums[0]

        for i in range(1, len(nums)):
            temp = maxP # 设置一个变量暂存最大值
            maxP = max(max(maxP * nums[i], nums[i]), minP * nums[i])
            minP = min(min(temp * nums[i], nums[i]), minP * nums[i])
            p = max(maxP, p)

        
        return p
    

if __name__ == "__main__":

    nums = [2, 3, -2, 4]
    sol = Solution()
    res = sol.maxProduct(nums)
    print(res)


